<template>
    <div class="wrap">
        <div class="login-wrap">
            <div class="login-title">商城后台管理-重置密码</div>
            <div id="darkbannerwrap"></div>
            <input type="text" autocomplete="off" id="read1" v-model="oldPassword" placeholder="旧密码">
            <input type="text" autocomplete="off" id="read2" v-model="newPWD" placeholder="新密码">
            <input type="text"   v-model="confirmNewPWD " placeholder="确认新密码" @keyup="confirmPass">
            <div class="code" style="position: relative;">
              <input type="text"  v-model="validCode" placeholder="请输入验证码" @keyup="confirmPass">
              <el-button @click="getCode" size="mini"
                         :disabled="!oldPassword || !newPWD || !confirmNewPWD || confirmNewPWD != newPWD"
              >{{text}}</el-button>
            </div>
            <div class="login-tips" v-if="tipToggle">两次输入不一致</div>
              <div class="login-button">
                  <button class="btn btn-primary" type="button" @click="login">确认修改</button>
              </div>
          </div>
    </div>
</template>

<script>
let CONSTANT = require('../../constant/constant.js'),
    common = require('../../common.js');
export default {
    name: 'login',
    data() {
        return {
            oldPassword:'',
            newPWD:'',
            confirmNewPWD :'',
            validCode: "",
            tipToggle:false,
            text: "获取验证码",
            times: 60,
        }
    },
    mounted(){
    },
    methods:{
        login(){
            if (!this.validCode) {
              this.$message({
                type: "warning",
                message: "请输入验证码"
              })
              return;
            }
            let url = CONSTANT.URL.ADMIN.UPDUSERPWD,
                data={
                    oldPWD:this.oldPassword,
                    newPWD:this.newPWD,
                    confirmNewPWD :this.confirmNewPWD,
                  validCode: this.validCode
                }
            // let param = JSON.stringify(data);
            let param = data;
            common.post(url,param,(res)=>{
                console.log(res)
               if(res.code == 200){
                    this.$message({
                        type: 'success',
                        message: '密码修改成功!'
                    });
                    setTimeout(()=>{
                        this.$router.push("/loginPage");
                    },1500)
                }else{
                    this.$message({
                        type: 'warning',
                        message: res.data.errMsg
                    });
                }
            })
        },
        confirmPass(){
//            if (!this.oldPassword || !this.newPWD || !this.confirmNewPWD || confirmNewPWD != newPWD)
            if(this.newPWD == this.confirmNewPWD ){
                this.tipToggle = false;
            }
            else{
                this.tipToggle = true;
            }
        },
        getCode() {
          if (this.times < 60) {
            return false
          }
          this._validCode()
        },
        _validCode() {
          let url = CONSTANT.URL.VERIFY.verify,
            timer,
            data={
              account: sessionStorage.getItem("mobile"),
              verifyCodeType: "ADMIN_RESETPWD"
            }
          let param = data;
          common.post(url,param,(res)=>{
            if(res.code == 200){
              timer = setInterval(() => {
                if (this.times > 0) {
                  this.times--
                  this.text = this.times + 's'

                } else {
                  clearInterval(timer)
                  this.times = 60
                  this.text = '获取验证码'
                }
              }, 1000)
              this.$message({
                type: 'success',
                message: '验证码发送成功!'
              });
            }else{
              clearInterval(timer)
              this.$message({
                type: 'warning',
                message: res.msg
              });
            }
          })
        }
    }
}
</script>

<style lang="scss" scoped>
.code {
  .el-button {
    position: absolute;
    top: 13px;
    right: 20px;
    background-color: #0E932E;
    color: #fff;
  }
}
.wrap {
    height:100%;
    background: url(../../images/bg.jpg) no-repeat center;
    position: fixed;
    width: 100%;
    left: 0;
    top: 0;
    right: 0;
    bottom: 0;
    background-size: cover;
    margin: 0px;
    padding: 0px;
    box-sizing: border-box;
    z-index: 999;
}

.login-wrap {
    position:relative;
    top:150px;
    margin: 0px auto 0 auto;
    min-height: 420px;
    max-width: 420px;
    padding: 40px;
    background-color: #ffffff;
    margin-left: auto;
    margin-right: auto;
    border-radius: 4px;
    /* overflow-x: hidden; */
    box-sizing: border-box;
}

.login-title {
    margin: 10px 0 0 -58px;
    padding: 18px 10px 18px 60px;
    background: #0E932E;
    position: relative;
    color: #fff;
    font-size: 16px;
}

#darkbannerwrap {
    background: url(../../images/aiwrap.png);
    width: 18px;
    height: 10px;
    margin: 0 0 20px -58px;
    position: relative;
}

input[type=text], input[type=file], input[type=password], input[type=email], select {
    border: 1px solid #DCDEE0;
    vertical-align: middle;
    border-radius: 3px;
    height: 50px;
    padding: 0px 16px;
    font-size: 14px;
    color: #555555;
    outline: none;
    width: 90%;
    margin-bottom: 25px;
    box-sizing: content-box;
}
.login-tips {
    margin-top: -20px;
    margin-bottom: 20px;
    text-align: right;
    font-size: 12px;
    color: red;
}
.login-button button{
    padding: 12px 24px;
    margin: 0px;
    font-size: 18px;
    line-height: 24px;
    text-align: center;
    white-space: nowrap;
    vertical-align: middle;
    width: 100%;
    cursor: pointer;
    color: #ffffff;
    background-color: #0E932E;
    border-radius: 3px;
    border: none;
}
</style>
